package bgu.eliav;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ViewItem extends Activity {
	static final int DIALOG_EXIT = 0;
	public String name = "";
	public String id = "";
	public String phone = "";
	public String address = "";
	public String bDate = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Activity viewItem = this;
		setContentView(R.layout.view_item);
		User userFromLastIntent = getIntent().getExtras().getParcelable(
				"bgu.eliav.extra");

		name = userFromLastIntent.getName();
		id = userFromLastIntent.getId();
		phone = userFromLastIntent.getPhoneNum();
		address = userFromLastIntent.getAddress();
		bDate = userFromLastIntent.getbDate();

		EditText text = (EditText) findViewById(R.id.EditName);
		text.setText(name);
		text.setEnabled(false);
		text.setClickable(false);
		text.setFocusable(false);

		text = (EditText) findViewById(R.id.EditID);
		text.setText(id);
		text.setEnabled(false);
		text.setClickable(false);
		text.setFocusable(false);

		text = (EditText) findViewById(R.id.EditPhone);
		text.setText(phone);
		text.setEnabled(false);
		text.setClickable(false);
		text.setFocusable(false);

		text = (EditText) findViewById(R.id.EditAddress);
		text.setText(address);
		text.setEnabled(false);
		text.setClickable(false);
		text.setFocusable(false);

		text = (EditText) findViewById(R.id.EditBdate);
		text.setText(bDate);
		text.setEnabled(false);
		text.setClickable(false);
		text.setFocusable(false);

		Button RemoveItemButton = (Button) this
				.findViewById(R.id.DeleteItemButton);
		RemoveItemButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remove from list and return to last view
				DatabaseExample db = DatabaseExample.getInstance(viewItem);
				db.open();
				db.deleteContactByUserId(id);
				db.close();
				finish(); // return to list view after the changes
			}
		});

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu1, menu);
		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			showDialog(DIALOG_EXIT);

		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_EXIT:
			return creatingExitDialog();

		}

		return dialog;
	}

	private Dialog creatingExitDialog() {
		final Activity localActivity = this;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?");
		builder.setCancelable(false);

		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				SharedPreferences ref = getSharedPreferences("bgu.eliav",
						MODE_PRIVATE);
				SharedPreferences.Editor ed = ref.edit();
				ed.putString("action", "exit");
				ed.commit();
				localActivity.finish();
			}
		});

		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;

	}

}
